package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.SelectShopAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.selectshopbean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_recycle.*
import kotlinx.android.synthetic.main.title_layout.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.util.*

class SelectShopActivity : BaseActivity(), IView {
    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_recycle
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "选择商户"
        iv_back.onClick { finish() }
        initRecyclerView()
    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val params = HashMap<String, String>()
        params["client"] = "android"
        params["key"] = token
        params["type"] = "2"
        datas.clear()
        RetrofitHelper.service.get_merchant(params).sss(this) {
            it.result.let { it1 -> datas.addAll(it1.merchant_list) }
            mAdapter.notifyDataSetChanged()
        }

    }


    /**
     * datas
     */
    private val datas = mutableListOf<selectshopbean.MerchantListBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: SelectShopAdapter by lazy {
        SelectShopAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {
            val data = datas[position]
            Intent().run {
                putExtra("shop_code", data.customerNo)
                putExtra("shop_name", data.shortName)
                setResult(300, this)
            }
            finish()
        }
    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            this.onItemClickListener = ItemClickListener
        }
    }

}